﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrumTime.Models;

namespace ScrumTime.Services
{
    public class UserService
    {
        ScrumTimeEntities _ScrumTimeEntities;
        AccountMembershipService _membershipService = new AccountMembershipService();

        public UserService(ScrumTimeEntities scrumTimeEntities)
        {
            _ScrumTimeEntities = scrumTimeEntities;
        }

        public UserService()
        {
            _ScrumTimeEntities = new ScrumTimeEntities();
        }

        public User GetUserByGUI(String gui)
        {
            User resultado = null;

            Guid user_guid = new Guid(gui);

            var results = from t in _ScrumTimeEntities.Users
                          where t.UserId == user_guid
                          select t;

            if (results.Count() > 0)
            {
                resultado = results.First<User>();
            }

            return resultado;
        }


        public static Product GetUserById(ScrumTimeEntities scrumTimeEntities, int id)
        {
            Product product = null;
            var results = from t in scrumTimeEntities.Products
                          where t.ProductId == id
                          select t;
            if (results.Count() > 0)
                product = results.First<Product>();
            else
                product = new Product();
            return product;
        }

        public bool userAssociatedWithAnotherProduct(String user_guid)
        {
            bool resultado = false;
            User user = GetUserByGuid(user_guid);
            if (user.Products.Count > 1)
            {
                resultado = true;
            }
            return resultado;
        }

        public User GetUserByGuid(String user_guid)
        {
            User resultado;
            resultado = GetUserByGUID(this._ScrumTimeEntities, user_guid);
            return resultado;
        }


        public static User GetUserByGUID(ScrumTimeEntities scrumTimeEntities, String id)
        {
            Guid guid = new Guid(id);

            User user = null;
            var results = from t in scrumTimeEntities.Users
                          where t.UserId == guid
                          select t;

            if (results.Count() > 0)
                user = results.First<User>();

            return user;
        }

        public String GetGuiByName(String name)
        {
            String resultado = String.Empty;

            name = name.ToLower();

            foreach (System.Web.Security.MembershipUser u in this._membershipService.GetAllUsers())
            {
                if (u.UserName.ToLower() == name)
                {
                    resultado = u.ProviderUserKey.ToString();
                    break;
                }
            }
            return resultado;
        }

        public void removeUserFromThisProduct(String user_guid, int productId)
        {
            User user = GetUserByGUI(user_guid);
            foreach (Product p in user.Products) {
                if (productId == p.ProductId) {
                    user.Products.Remove(p);
                    break;
                }
            }
            _ScrumTimeEntities.ObjectStateManager.ChangeObjectState(user, System.Data.EntityState.Modified);
            this._ScrumTimeEntities.SaveChanges();
        }

        public void removeUserFromAllProduct(String user_guid)
        {
            User user = GetUserByGUI(user_guid);
            user.Products.Clear();
            _ScrumTimeEntities.ObjectStateManager.ChangeObjectState(user, System.Data.EntityState.Modified);
            this._ScrumTimeEntities.SaveChanges();
        }

        public bool isUserAdmin() {
            return false;
        }

        public List<String> GetGUIUserProducts(Product prod)
        {
            List<String> resultado = new List<String>();            
            if ( prod != null){
                foreach (ScrumTime.Models.User u in prod.Users)
                {
                    resultado.Add(u.UserId.ToString());
                }
            }
            return resultado;
        }


        public List<String> GetGUIUserProducts(int productId)
        {
            Product p = GetProduct(productId);
            return GetGUIUserProducts(p);
        }

        private Product GetProduct( int productId){
            Product resultado = null;
            foreach (Product p in _ScrumTimeEntities.Products)
            {
                if (p.ProductId == productId)
                {
                    resultado = p;
                    break;
                }
            }
            return resultado;
        }

        

    }

}